package de.tu_darmstadt.gdi1.battleship.view;

import java.io.IOException;

import javax.swing.JDialog;

import de.tu_darmstadt.gdi1.battleship.exceptions.InternalFailureException;
import de.tu_darmstadt.gdi1.battleship.exceptions.InvalidOperationException;
import de.tu_darmstadt.gdi1.battleship.exceptions.ParameterOutOfRangeException;
import de.tu_darmstadt.gdi1.battleship.ui.GamePanel;
import de.tu_darmstadt.gdi1.battleship.ui.GameWindow;

public class StudentPanel extends GamePanel {

	StudentWindow parentWindow;

	public StudentPanel(StudentWindow theParentWindow) {
		super(theParentWindow);
		parentWindow = theParentWindow;
		setAutosize(true);
	}

	@Override
	protected void setGamePanelContents() {

		loadSkin();

		if (parentWindow.getBsg() != null && parentWindow.getBsg().getHumanMap() != null
				&& parentWindow.getBsg().getCpuMap() != null) {
			try {
				for (int r = 0; r < 10; r++) {
					for (int c = 0; c < 10; c++) {
						placeEntity(((Character) parentWindow.getBsg().getHumanMap()[c][r]
								.getSymbol()).toString());
					}
					placeEntity("|");
					for (int c = 0; c < 10; c++) {
						placeEntity(((Character) parentWindow.getBsg().getCpuMap()[c][r]
								.getSymbol()).toString());
					}

				}
			} catch (ParameterOutOfRangeException e) {
				System.err.println("Map contains invalid symbols.");
			} catch (NullPointerException e) {
				System.err.println("Error in map, maybe no map loaded?");
			}
		}

//		try {
//			notifyLevelLoaded(20, 10);
//		} catch (ParameterOutOfRangeException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (InternalFailureException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}

	}

	private void loadSkin() {

		// Load skin
		Skin defaultSkin = new Skin();

		// Try to load the images
		for (String str : defaultSkin.getImages().keySet()) {
			try {
				if (!isImageRegistered(str))
					registerImage(str, defaultSkin.getImages().get(str));
			} catch (ParameterOutOfRangeException e) {
				e.printStackTrace();
			} catch (InvalidOperationException e) {
				e.printStackTrace();
			} catch (InternalFailureException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void entityClicked(int positionX, int positionY) {
		
		parentWindow.getController().userClicked(positionX, positionY);

	}

	@Override
	protected void panelResized() {
		// TODO cleanup that dirty stuff
		
		parentWindow.setSize(this.getWidth() , this.getHeight() + 20);

	}

}
